package com.dd.front.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dd.common.config.GlobalConfig;
import com.dd.common.convert.UserConvert;
import com.dd.common.dto.user.UserUpdateDTO;
import com.dd.common.entity.User;
import com.dd.common.enums.ErrorEnum;
import com.dd.common.exception.BusinessException;
import com.dd.common.pojo.dto.UpdatePasswordDTO;
import com.dd.common.service.UserService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.StrGenerator;
import com.dd.common.validator.annotation.IDMust;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@SaIgnore
@Tag(name = "用户")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserConvert userConvert;

    @Operation(summary = "个人详情", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "用户ID"))
    @GetMapping("/detail/{id}")
    public AjaxResult<?> detail(@PathVariable @IDMust Long id) {
        return AjaxResult.success(userService.getById(id));
    }

    @Operation(summary = "修改密码", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UpdatePasswordDTO.class))
    ))
    @PostMapping("/updatePassword")
    public AjaxResult<?> updatePassword(@Validated @RequestBody UpdatePasswordDTO dto) {
        if (!dto.getNewPassword().equals(dto.getConfirmPassword())) {
            throw new BusinessException(ErrorEnum.PASSWORD_ERROR, "新密码和确认密码不一致");
        }
        User currentUser = (User) StpUtil.getSession().get(GlobalConfig.CURRENT_USER);
        if (!currentUser.getPassword().equals(StrGenerator.makeMd5(dto.getOldPassword()))) {
            throw new BusinessException(ErrorEnum.OLD_PASSWORD_ERROR);
        }
        userService.update(new LambdaUpdateWrapper<User>()
                .eq(User::getId, currentUser.getId())
                .set(User::getPassword, StrGenerator.makeMd5(dto.getNewPassword())));
        return AjaxResult.success(true);
    }

    @Operation(summary = "信息编辑", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UserUpdateDTO.class))
    ))
    @PostMapping("/updateProfile")
    public AjaxResult<?> updateProfile(@Validated @RequestBody UserUpdateDTO dto) {
        return AjaxResult.success(userService.updateById(userConvert.dtoToEntity(dto)));
    }
}
